﻿using MediatR;
using ProcurementManagement.Applcation.Command.QualityCommand;
using ProcurementManagement.Domain.Dtos.Quality;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Dtos;
using ProcurementManagement.Infrstructre.Interface;
using SqlSugar;

namespace ProcurementManagement.Applcation.CommandHanlers.QualityCommandHandler
{
    public class SelectProPcApplyCommandHandler:IRequestHandler<SelectProPcApplyCommand, PageDTO<SelectProPcApplyDTO>>
    {
        private readonly IProPcApplyRepository repository;
        private readonly IPurchaseContractsRepository _purchaseContractsRep;
        private readonly ISourcProductContractRepository _sourceProductContractRep;
        private readonly InterfaceVendorRepository _vendorRep;

        ISqlSugarClient db;


        public SelectProPcApplyCommandHandler(IProPcApplyRepository repository, IPurchaseContractsRepository purchaseContractsRep, ISqlSugarClient db , ISourcProductContractRepository sourceProductContractRep, InterfaceVendorRepository vendorRep)
        {
            this.repository = repository;
            _purchaseContractsRep = purchaseContractsRep;
            this.db = db;
            _sourceProductContractRep = sourceProductContractRep;
            _vendorRep = vendorRep;
          
        }

        public async Task<PageDTO<SelectProPcApplyDTO>> Handle(SelectProPcApplyCommand request, CancellationToken cancellationToken)
        {

            var list = db.Queryable<ProPcApply, PurchaseContracts, SourcProductContract, Vendor>((pq, pc, sp, Vendor) => new JoinQueryInfos(
                JoinType.Inner, pq.PurchaseContractsIds == pc.PurchaseContractsIds,
                JoinType.Inner, pc.PCNumber == sp.PCNumber,
                JoinType.Inner, pc.VendorId == Vendor.VendorId))
            
                .Where((pq, pc, sp, Vendor) => pq.IsDel == false)
                .GroupBy((pq, pc, sp, Vendor) => new
                {
                    
                    pq.ProApplyId,
                    pc.PurchaseContractsIds,
                    pq.ProcureInspectionId,
                    pq.ManpowerId,
                    pc.PCName,
                    pq.QcTopic,
                    pq.QcCode,
                    pq.QcTime,
                    pq.QcPerson,
                    sp.SPCNum,
                    sp.SPCPrice,
                    sp.SPCAfterPrice,
                    sp.SPCArrivalDate,
                    Vendor.VName,
                    pq.AddName,
                    pq.AddTime,
                    pq.IsDel,
                })
                .Select((pq, pc, sp, Vendor) => new SelectProPcApplyDTO 
            {
                ProApplyId = pq.ProApplyId,
                PurchaseContractsIds = pc.PurchaseContractsIds,
                ProcureInspectionId = pq.ProcureInspectionId,
                ManpowerId = pq.ManpowerId,
                ProContract = pc.PCName,
                QcTopic = pq.QcTopic,
                QcCode = pq.QcCode,
                QcPerson = pq.QcPerson,
                QcTime = pq.QcTime,
                SPCNum = sp.SPCNum,
                SPCPrice = sp.SPCPrice,
                SPCTotalPrice = sp.SPCAfterPrice,
                SPCArrivalDate = sp.SPCArrivalDate,
                VName  = Vendor.VName,
                AddName = pq.AddName,
                AddTime = pq.AddTime,
                IsDel = pq.IsDel,

            }).ToList();

            if (!string.IsNullOrEmpty(request.Vname))
            {

                list = list.Where(x=> x.VName.Contains(request.Vname)).ToList();
            }
            if (request.PCId > 0)
            {
                list = list.Where(x => x.PurchaseContractsIds == request.PCId).ToList();
            }


           

            var totalcount =  list.Count();
            var pagedto =  list.OrderByDescending(pq => pq.ProApplyId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            

           var pages =new PageDTO<SelectProPcApplyDTO>
           {
                Totalcount= totalcount,
               Pagecount = (int)Math.Ceiling(totalcount*1.0/request.PageSize),
               list = pagedto
           };

            return pages;
        }
    }
}
